package com.ludomancy.sos64.components;

import java.awt.Graphics;
import java.awt.Point;
import java.util.Iterator;
import java.util.Vector;

import com.ludomancy.gamelib.ImageAnimation;
import com.ludomancy.gamelib.ImageStrip;
import com.ludomancy.gamelib.ResourceManager;
import com.ludomancy.sos64.Component;
import com.ludomancy.sos64.Game;

public class Light extends Component
{
	private ImageAnimation lights;
	private int powerLevel;
	
	public Light(Point pos, int powerLevel)
	{
		super(pos.x,pos.y);
		this.powerLevel = powerLevel;
		if(lights == null)
		{
			lights = new ImageAnimation(new ImageStrip(ResourceManager.getImage("lights.png"), 15, 19), 4);
			lights.setSequenceBouncingLoop();
			lights.looping = true;
		}
	}
	
	public Point getSize()
	{
		return new Point(15,19);
	}

	public void paint(Graphics g, int px, int py)
	{
		lights.paintCurrentFrame(g, px, py);
	}
	
	public boolean isMadeOfMetal()
	{
		return true;
	}
	
	public boolean canMove()
	{
		return false;
	}

	public void update()
	{
		lights.update();
		int power = 0;
		Vector turbines = Game.game.getComponents(Turbine.class);
		for (Iterator iterator = turbines.iterator(); iterator.hasNext();)
		{
			Turbine c = (Turbine) iterator.next();
			if(c.generating)
			{
				power++;
			}
		}
		
		if(power > powerLevel)
		{
			lights.play();
			if(powerLevel == 2)
			{
				// light up the battery!
				Vector batteries = Game.game.getComponents(Battery.class);
				for (Iterator iterator = batteries.iterator(); iterator.hasNext();)
				{
					Battery c = (Battery) iterator.next();
					c.isFull = true;
				}
			}
		}
		else
		{
			lights.stop();
			lights.rewind();
		}
	}
}
